/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package platform;

import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import zorg.CryptoException;
import zorg.platform.EncryptorSuite;

/**
 *
 * @author Tomasz Nowak
 */
public class TenorEncryptorSuite implements EncryptorSuite
{
	byte[] key;
	byte[] initVector;

	TenorEncryptorSuite( byte[] key_, byte[] initVector_ )
	{
		key = key_;
		initVector = initVector_;
	}

	@Override
	public void encrypt( byte[] aIV, byte[] buffer ) throws CryptoException
	{
		try
		{
			Cipher cipher;
			cipher = Cipher.getInstance("AES/CBC/NoPadding");
			cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "AES"), new IvParameterSpec(initVector));
			byte[] result = cipher.doFinal(aIV);

			System.arraycopy(result, 0, buffer, 0, buffer.length);
		}
		catch ( NoSuchAlgorithmException ex )
		{
			Logger.getLogger(TenorCryptoUtils.class.getName()).log(Level.ALL, null, ex);
		}
		catch ( NoSuchPaddingException ex )
		{
			Logger.getLogger(TenorCryptoUtils.class.getName()).log(Level.ALL, null, ex);
		}
		catch ( InvalidKeyException ex )
		{
			Logger.getLogger(TenorCryptoUtils.class.getName()).log(Level.ALL, null, ex);
		}
		catch ( InvalidAlgorithmParameterException ex )
		{
			Logger.getLogger(TenorCryptoUtils.class.getName()).log(Level.ALL, null, ex);
		}
		catch ( IllegalBlockSizeException ex )
		{
			Logger.getLogger(TenorCryptoUtils.class.getName()).log(Level.ALL, null, ex);
		}
		catch ( BadPaddingException ex )
		{
			Logger.getLogger(TenorCryptoUtils.class.getName()).log(Level.ALL, null, ex);
		}
	}

	@Override
	public byte[] encryptIV_for_prf( byte[] IV ) throws CryptoException
	{
		try
		{
			Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
			cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "AES"), new IvParameterSpec(initVector));
			return cipher.doFinal(IV);
		}
		catch ( NoSuchAlgorithmException ex )
		{
			Logger.getLogger(TenorCryptoUtils.class.getName()).log(Level.SEVERE, null, ex);
		}
		catch ( NoSuchPaddingException ex )
		{
			Logger.getLogger(TenorCryptoUtils.class.getName()).log(Level.SEVERE, null, ex);
		}
		catch ( InvalidKeyException ex )
		{
			Logger.getLogger(TenorCryptoUtils.class.getName()).log(Level.SEVERE, null, ex);
		}
		catch ( InvalidAlgorithmParameterException ex )
		{
			Logger.getLogger(TenorCryptoUtils.class.getName()).log(Level.SEVERE, null, ex);
		}
		catch ( IllegalBlockSizeException ex )
		{
			Logger.getLogger(TenorCryptoUtils.class.getName()).log(Level.SEVERE, null, ex);
		}
		catch ( BadPaddingException ex )
		{
			Logger.getLogger(TenorCryptoUtils.class.getName()).log(Level.SEVERE, null, ex);
		}
		return null;
	}
}
